package it.unibo.android.convoycruisecontrol.boatsubsystem;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import it.unibo.android.util.map.MyItemizedOverlay;
import it.unibo.convoycruisecontrol.boatsubsystem.IDashboard;
import it.unibo.convoycruisecontrol.boatsubsystem.ObservableDashboard;
import it.unibo.convoycruisecontrol.progettodesignandroid.R;
import it.unibo.convoycruisecontrol.progettodesignandroid.R.drawable;
import it.unibo.convoycruisecontrol.progettodesignandroid.R.id;
import it.unibo.convoycruisecontrol.progettodesignandroid.R.layout;
import it.unibo.location.ILocation;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

public class MyMapView extends LinearLayout implements Observer {

	private ObservableDashboard dashboard;
	private MapView mapView;
	private MapController mc;

	private List<Overlay> mapOverlays;
	private Drawable drawable;

	public MyMapView(Activity activity, ObservableDashboard dashboard) {
		super(activity, null);

		this.dashboard = dashboard;
		initGui(activity);
		dashboard.addObserver(this);
		mapView.setBuiltInZoomControls(true);
	}

	private void initGui(Activity activity) {
		LayoutInflater layoutInflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.map, this);
		mapView = (MapView) findViewById(R.id.map_view);
		this.mc = mapView.getController();
		
		mapOverlays = mapView.getOverlays();
		drawable = this.getResources().getDrawable(R.drawable.ic_launcher);

		MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(drawable);

		for (ILocation location : this.dashboard.getDisplayedLocations()) {
			GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),
					(int) (location.getLongitude() * 1E6));
			OverlayItem overlayitem = new OverlayItem(point, "Hello",
					"I'm a boat!");
			itemizedOverlay.addOverlay(overlayitem);
		}
		mapOverlays.add(itemizedOverlay);

		animateToFirstLocation();
		
		mc.setZoom(10);

	}

	private void animateToFirstLocation() {
		/* Animazione della mappa alla prima posizione */
		if (!this.dashboard.getDisplayedLocations().isEmpty())
			mc.animateTo(new GeoPoint((int) ((this.dashboard
					.getDisplayedLocations()).get(0).getLatitude() * 1E6),
					(int) ((this.dashboard.getDisplayedLocations()).get(0)
							.getLongitude() * 1E6)));
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		mapOverlays.clear();
		MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(drawable);

		for (ILocation location : this.dashboard.getDisplayedLocations()) {
			GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),
					(int) (location.getLongitude() * 1E6));
			OverlayItem overlayitem = new OverlayItem(point, "Hello",
					"I'm a boat!");
			itemizedOverlay.addOverlay(overlayitem);
		}
		mapOverlays.add(itemizedOverlay);
		
		animateToFirstLocation();
	}
}
